﻿using System;

namespace EasyEvent
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("简单Event测试");
            BroadCaster broadCaster = new BroadCaster();
            broadCaster.PriceChanged += BroadCaster_PriceChanged;
            Console.WriteLine("设定初始价格...");
            broadCaster.Price = 100;
            Console.WriteLine("设定新的价格");
            broadCaster.Price = 101.35m;

        }

        private static void BroadCaster_PriceChanged(decimal oldPrice, decimal newPrice)
        {
            Console.WriteLine($"旧的价格是{oldPrice},新价格是{newPrice}");
        }
    }

    public delegate void PriceChangedHandler(decimal oldPrice, decimal newPrice);

    //广播
    public class BroadCaster
    {
        decimal price;
        public decimal Price
        {
            get { return price; }
            set
            {
                if (Price == value)
                    return;
                decimal oldPrice = price;
                price = value;
                PriceChanged?.Invoke(oldPrice, price);
            }
        }
        public  PriceChangedHandler PriceChanged;
    }
}
